package com.swift.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;

public class EncryptUtils {

	public static String encryptDES(String encryptString, String encryptKey) {
		try {
			// 返回实现指定转换的 Cipher 对象 “算法/模式/填充”
			Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			byte[] encryptKeys = trim(encryptKey.getBytes("UTF-8"),  8); 
			// 创建一个 DESKeySpec 对象，使用 8 个字节的key作为 DES 密钥的密钥内容。
			DESKeySpec desKeySpec = new DESKeySpec(encryptKeys);
			// 返回转换指定算法的秘密密钥的 SecretKeyFactory 对象。
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			// 根据提供的密钥生成 SecretKey 对象。
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
			// 使用 iv 中的字节作为 IV 来构造一个 IvParameterSpec 对象。复制该缓冲区的内容来防止后续修改。
			IvParameterSpec iv = new IvParameterSpec(encryptKeys);
			// 用密钥和一组算法参数初始化此 Cipher;Cipher：加密、解密、密钥包装或密钥解包，具体取决于 opmode 的值。
			cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
			// 加密同时解码成字符串返回
			return new String(Base64.encode(cipher.doFinal(encryptString.getBytes("UTF-8")),Base64.DEFAULT));
		} catch (Exception e) {
			return null;
		}
	}

	public static String decryptDES(String decodeString, String decodeKey) {
		try {
			byte[] decodeKeys = trim(decodeKey.getBytes("UTF-8"),  8); 
			// 使用指定密钥构造IV
			IvParameterSpec iv = new IvParameterSpec(decodeKeys);
			// 根据给定的字节数组和指定算法构造一个密钥。
			SecretKeySpec skeySpec = new SecretKeySpec(decodeKeys, "DES");
			// 返回实现指定转换的 Cipher 对象
			Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			// 解密初始化
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
			// 解码返回
			byte[] byteMi = Base64.decode(decodeString, Base64.DEFAULT);
			byte decryptedData[] = cipher.doFinal(byteMi);
			return new String(decryptedData);
		} catch (Exception e) {
			return null;
		}
	}
	
	public static byte[] trim(byte[] source, int length){
		byte[] result = new byte[length];
		if (source.length < length){
			System.arraycopy(source, 0, result, 0, source.length);
		}else{
			System.arraycopy(source, 0, result, 0, length);
		}
		return result;
	}
	
	public static String MD5(String info)
	{
	  try
	  {
	    MessageDigest md5 = MessageDigest.getInstance("MD5");
	    md5.update(info.getBytes("UTF-8"));
	    byte[] encryption = md5.digest();
	      
	    StringBuffer strBuf = new StringBuffer();
	    for (int i = 0; i < encryption.length; i++)
	    {
	      if (Integer.toHexString(0xff & encryption[i]).length() == 1)
	      {
	        strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
	      }
	      else
	      {
	        strBuf.append(Integer.toHexString(0xff & encryption[i]));
	      }
	    }
	      
	    return strBuf.toString();
	  }
	  catch (NoSuchAlgorithmException e)
	  {
	    return "";
	  }
	  catch (UnsupportedEncodingException e)
	  {
	    return "";
	  }
	}

}
